package net.sahv.bdyz.controller.admin;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import net.sahv.bdyz.model.Admin;
import net.sahv.bdyz.model.AdminRole;
import net.sahv.bdyz.service.AdminRoleService;
import net.sahv.bdyz.service.AdminService;
import net.sahv.bdyz.service.OrdersListService;
import net.sahv.bdyz.service.OrdersService;
import net.sahv.bdyz.util.LoadCfgUtil;
import net.sahv.bdyz.util.PagingUtil;

@Controller
@RequestMapping("admin/orders/")
public class OrdersadminController {
	@Autowired
	private OrdersService ordersService;
	@Autowired
	private OrdersListService orderListsService;
	@Autowired
	private AdminService adminService;
	@Autowired
	private AdminRoleService adminRoleService;// 角色管理员关系

	/**
	 * 跳转查询页面
	 * @author 薛国计
	 * @date 2016年8月9日
	 * @return
	 */
	@RequestMapping(value = "listorders", method = RequestMethod.GET)
	public String listorders(){
		return "admin/orders/listorders";
	}
	/**
	 * 查询数据
	 * @author 薛国计
	 * @date 2016年8月9日
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "listorders", method = RequestMethod.POST)
	public JSON listorders(PagingUtil pagingUtil, HttpServletRequest request,String O_NO,String A_NAME) {
		Admin admin = adminService.getCurrent();// 获取当前登录管理员
		Map<String, Object> map = new HashMap<String, Object>();
		if (admin != null) {
			AdminRole ar = adminRoleService.selectByAid(admin.getaId());
			int roleId = 0;
			if (ar != null) {
				roleId = ar.getrId();
			}
			String sortField = " order by orders.O_ID desc";
			String where = " where 1=1 ";
			if (roleId == 2) {
				where += " and ad.A_ID=" + admin.getaId();
			}
			if (O_NO != null && !O_NO.trim().equals("")) {
				where += " AND orders.O_NO LiKE '%" + O_NO + "%' ";
			}
			if (A_NAME != null && !A_NAME.trim().equals("")) {
				where += " AND ad.`A_NAME` LIKE '%" + A_NAME + "%' ";
			}
			Map<String, Object> param = new HashMap<String, Object>();

			param.put("start", pagingUtil.getiDisplayStart());
			param.put("length", pagingUtil.getiDisplayLength());
			param.put("where", where);
			param.put("sortField", sortField);

			List<Map<String, Object>> ffList = ordersService.getList(param);
			// 统计信息的总条数
			Integer totalCount = ordersService.getTotalCount(where);

			map.put("aaData", ffList);
			// 查出来总共有多少条记录
			map.put("iTotalRecords", totalCount);
			map.put("iTotalDisplayRecords", totalCount);
			map.put("sEcho", pagingUtil.getsEcho());
		} else {
			map.put("aaData", "");
			// 查出来总共有多少条记录
			map.put("iTotalRecords", 0);
			map.put("iTotalDisplayRecords", 0);
			map.put("sEcho", pagingUtil.getsEcho());
		}
		return  (JSON) JSON.toJSON(map);
	}

	/**
	 * 删除订单
	 * @author 薛国计
	 * @date 2016年8月9日
	 * @param request
	 * @param id 订单id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "delorders", method = RequestMethod.POST)
	public ApiResult delorders(HttpServletRequest request, String[] id) {
		ApiResult result = new ApiResult();
		for(String item:id){
			
		int i = ordersService.deleteByPrimaryKey(Integer.valueOf(item));
		if (i > 0) {
			orderListsService.deleteByoId(Integer.valueOf(item));
		}
		}
		return result;

	}

	/**
	 * 查看订单
	 * @author 薛国计
	 * @date 2016年8月9日
	 * @param oId
	 * @param model
	 * @return
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	@RequestMapping(value = "vieworders", method = RequestMethod.GET)
	public String vieworders(String id, Model model) throws FileNotFoundException, IOException {
		
		Map<String, String> mapUrls = LoadCfgUtil.loadCfg();
		model.addAttribute("imgpath",mapUrls.get("targetUrl.img"));
		model.addAttribute("orders", ordersService.selectByPrimaryKey(Integer.valueOf(id)));
		model.addAttribute("orderslist", orderListsService.selectAllByOrdersId(Integer.valueOf(id),"ch"));
		return "admin/orders/vieworders";
	}

}
